﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Windows.Forms;
using LPV_POS.mh_pos;

namespace LPV_POS.In
{
    class print_BIEN_NHAN
    {
        #region ---------------begin IN BILL TINH DIEM KHACH HANG---------------
        public Boolean Print_PHIEU_BIENNHAN(string _maPhieu)
        {
            #region "   LoadData BH_PHIEU-BH_PHIEU_CT"
            Variable oVariable = new Variable();
            oVariable.Get_DM_MAYIN_BIENNHAN();
            //Mo cong com
            DataRow _drPhieu = null;
            DataTable _tbPhieuCT = null;
            DataTable _tbPhieu = this.getTable_Phieu("", _maPhieu + "");
            string maPhieu = "";

            if (_tbPhieu.Rows.Count == 1)
                maPhieu = _tbPhieu.Rows[0]["OBJID"] + "";
            else
                return false;

            if (_tbPhieu.Select("OBJID LIKE '" + maPhieu + "'").Length > 0)
                _drPhieu = _tbPhieu.Select("OBJID LIKE '" + maPhieu + "'")[0];
            // show tra hang
            string sSQL = "";
            sSQL =
                "SELECT MIN(aa.OBJID), aa.OBJNAME, aa.BH_PHIEU_MA, aa.MAHH, SO_LUONG = SUM(aa.SO_LUONG), aa.DVT, DON_GIA = aa.DON_GIA,  " +//-- MAX
                "       CHIETKHAU_PHANTRAM = MAX(aa.CHIETKHAU_PHANTRAM), CHIETKHAU_TIEN = MAX(aa.CHIETKHAU_TIEN), THANH_TIEN = SUM(aa.THANH_TIEN),  " +
                "       VAT_TIEN = MAX(aa.VAT_TIEN), VAT_PHANTRAM, aa.GHI_CHU " +
                "FROM  " +
                "(  " +
                "    SELECT b.OBJID, OBJNAME = ISNULL(h.OBJNAME, b.MAHH), " +//"(CASE WHEN b.MAHH LIKE 'Tien_Gio%' THEN N'Tiền giờ' ELSE h.OBJNAME END ), "+
                "           b.BH_PHIEU_MA, " +
                "           b.MAHH, b.SO_LUONG, b.DVT, b.DON_GIA, b.CHIETKHAU_PHANTRAM, " +
                "           b.CHIETKHAU_TIEN, b.THANH_TIEN, b.VAT_PHANTRAM, b.VAT_TIEN, b.GHI_CHU " +
                "    FROM BH_PHIEU_CT b " +
                "        LEFT JOIN DM_HH h ON b.MAHH = h.OBJID  " +
                "    WHERE b.BH_PHIEU_MA LIKE '" + maPhieu + "'  " +
                ")aa " +
                "GROUP BY aa.OBJNAME, aa.BH_PHIEU_MA, aa.MAHH, aa.DVT, aa.VAT_PHANTRAM, aa.DON_GIA, aa.GHI_CHU " +
                //     "HAVING SUM(aa.SO_LUONG) > 0 " +  // show mat hang tra lai trong bill thanh toan
                "ORDER BY MIN(aa.OBJID)";

            _tbPhieuCT = new thuvien().SP_SelectDataTable("SP_EXECUTESQL", new string[] { "@sSQL" }, new object[] { sSQL });
            #endregion

            if (_drPhieu != null)
            {
                if (oVariable.SerialPort_Open_Receipt())//mở cổng COM
                {
                    foreach (SerialPort_LPV oSerialPort_LPV in Variable.gsrlCOMBillport)
                    {
                        if (oSerialPort_LPV != null)
                        {
                            if (!oSerialPort_LPV.IsOpen)
                                oSerialPort_LPV.Open();

                            this.Print_PHIEU_BIENNHAN(oSerialPort_LPV, _drPhieu, _tbPhieuCT);

                            #region "mo ket"
                            Boolean _moket = false;
                            Boolean.TryParse(Variable.tbBH_CAU_HINH.Rows[0]["MO_KET"] + "", out _moket);
                            if (_moket == true)
                                oSerialPort_LPV.Write(((char)(27)).ToString() + "p" + '\0' + ((char)(100)).ToString() + ((char)(250)).ToString());
                            #endregion

                            if (oSerialPort_LPV.IsOpen)
                                oSerialPort_LPV.Close();

                        }

                    }
                }
            }
            new Variable().SerialPort_Close(); //Đóng cổng COM
            return true;

        }

        private void Print_PHIEU_BIENNHAN(System.IO.Ports.SerialPort psrlCOMport, DataRow _drPhieu, DataTable _tbPhieuCT)
        {
            #region "   Print_PHIEU_BIENNHAN"
            try
            {
                cpublic ocpublic = new cpublic();
                #region "language: EN_VN"
                string sLableBan_Phong = "Table";
                string sLableNgayGio_Vao = "Date in";
                string sLableNgayGio_Ra = "Date out";
                string sLableHoaDon = "No";
                string sLableHangHoa = "Item";
                string sLableTienPhong_Gio = "";
                string sLableTienGio_Them = "";
                string sLableGia = "Price";
                string sLableSoLuong = "Qty";
                string sLableThanhTien_CT = "Amount";
                string sLableGiamGia = "Discount";
                string sLableVAT = "Vat";
                string sLableTongTien1 = "Net total";
                string sLableTienTIPs = "Tip";
                string sLablePhiDichVu = "Service charge";
                string sLablePhuThuKhac = "Other surcharge";
                string sLableTienDatCoc = "Deposit";
                string sLableTongTien2 = "Final total";
                string sLableNhanVien = "Cashier";
                string sLableKhachHang = "Customer";
                string sLableBillInLai = "Bill reprint";
                string sLableDaNhan = "Received";
                string sLableConLai = "Refund";
                string sLableKhuyenMai = "Promotions";
                string sLableDienThoai = "Tel";

                ////////TIENG VIET
                //IF(!BH_CAU_HINH.IN_BILL_TIENG_ANH)
                #region "   ko xai"
                //if (_drPhieu["DM_LOAIDICHVU_MA"] + "" == "1")//Karaoke
                //{
                //    sLableTienPhong_Gio = "Tien gio";
                //    sLableBan_Phong = "Phong";
                //}
                //else if (_drPhieu["DM_LOAIDICHVU_MA"] + "" == "2")//Hotel
                //{
                //    sLableTienPhong_Gio = "Tien phong";
                //    sLableBan_Phong = "Phong";
                //}
                //else
                //{
                //    sLableTienPhong_Gio = "Tien gio";
                //    sLableBan_Phong = "Ban";
                //}
                #endregion
                if (Variable.tbDM_LOAIDICHVU != null)
                {
                    if (_drPhieu["DM_LOAIDICHVU_MA"] + "" != "")
                    {
                        sLableTienPhong_Gio = Variable.tbDM_LOAIDICHVU.Select("OBJID=" + _drPhieu["DM_LOAIDICHVU_MA"])[0]["TIEN_PHONG_GIO"] + "";
                        sLableBan_Phong = Variable.tbDM_LOAIDICHVU.Select("OBJID=" + _drPhieu["DM_LOAIDICHVU_MA"])[0]["BAN_PHONG"] + "";
                    }
                }

                sLableTienPhong_Gio = ocpublic.RemoveSign4VietnameseString(sLableTienPhong_Gio);                
                sLableBan_Phong = ocpublic.RemoveSign4VietnameseString(sLableBan_Phong);
                if (ocpublic.Registry_Get("chk_PrintBill_COM_English") != "True")
                {
                    sLableNgayGio_Vao = "Ngay gio vao";
                    sLableNgayGio_Ra = "Ngay gio ra";
                    sLableHoaDon = "HD";
                    sLableHangHoa = "Hang hoa";
                    sLableGia = "Gia";
                    sLableSoLuong = "SL";
                    sLableThanhTien_CT = "Tong cong";
                    sLableGiamGia = "Giam gia";
                    sLableVAT = "Vat";
                    sLableTongTien1 = "Tong cong";
                    sLableTienTIPs = "Tien TIP";
                    sLablePhiDichVu = "Phi dich vu";
                    sLablePhuThuKhac = "Phu thu khac";
                    sLableTienDatCoc = "Dat coc";
                    sLableTongTien2 = "Thanh tien";
                    sLableNhanVien = "Nhan vien";
                    sLableKhachHang = "Khach hang";
                    sLableBillInLai = "Hoa don in lai";
                    sLableDaNhan = "Tien khach dua";
                    sLableConLai = "Tien thua";
                    sLableKhuyenMai = "Khuyen mai";
                    sLableDienThoai = "Dt";
                }
                #endregion
                //
                mh_dmkhachhang omh_dmkhachhang = new mh_dmkhachhang();
                string skhachhang_op = ocpublic.Registry_Get(omh_dmkhachhang.Name + omh_dmkhachhang.txtkhachhang_optioninbill.Name);
                if (skhachhang_op != "")
                    sLableKhachHang = skhachhang_op;
                //
                #region "   KHAI BAO BIEN..."
                // -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# 
                // -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# 
                string Print_Align_Center = ((char)(27)).ToString() + ((char)(97)).ToString() + ((char)(1)).ToString();
                string Print_Double_Height = ((char)(27)).ToString() + ((char)(33)).ToString() + ((char)(16)).ToString();
                string Print_Align_Left = ((char)(27)).ToString() + ((char)(97)).ToString() + '\0';

                //string Print_Font_Normal_40CharPerRow = ((char)(27)).ToString() + ((char)(33)).ToString() + ((char)(1)).ToString();
                string Print_Font_Normal_39CharPerRow = ((char)(27)).ToString() + ((char)(33)).ToString() + ((char)(2)).ToString();
                string Print_Font_Double_Height = ((char)(27)).ToString() + ((char)(33)).ToString() + ((char)(16)).ToString();


                // -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# 
                //string TITLE = "";
                //string ADDRESS = "";
                //string PHONE = "";
                // -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# 
                //DataTable tbSETPARA = null;
                #endregion

                #region "   tbSETPARA"
                //string sSQL = "SELECT * FROM SETPARA";
                //tbSETPARA = new thuvien().SP_SelectDataTable("SP_EXECUTESQL", new string[] { "@sSQL" }, new object[] { sSQL });

                //if (tbSETPARA.Rows.Count > 0)
                //{
                //    TITLE = tbSETPARA.Rows[0]["LEFTTOPLINE1"] + "";
                //    ADDRESS = tbSETPARA.Rows[0]["LEFTTOPLINE2"] + "";
                //    PHONE = tbSETPARA.Rows[0]["ORGTEL"] + "";
                //    if (PHONE != "")
                //        PHONE = "SDT: " + PHONE;
                //}
                #endregion

                #region "   tbDM_MAYIN_BienNhan_CAUHINH"
                string LOGO_PATH = "";
                string HEADER = "";
                string FOOTER = "";
                string TITLE = "";
                string ADDRESS = "";
                string PHONE = "";
                int MARIN_TOP = 0;
                int MARIN_BOTTOM = 0;

                Boolean PRINT_HIEN_THI_TIEN_THOI = false;
                Boolean PRINT_HIEN_THI_TIEN_KHACH_DUA = false;
                Boolean.TryParse(Variable.tbBH_CAU_HINH.Rows[0]["HIEN_THI_TIEN_THOI"] + "", out PRINT_HIEN_THI_TIEN_THOI);
                Boolean.TryParse(Variable.tbBH_CAU_HINH.Rows[0]["HIEN_THI_TIEN_KHACH_DUA"] + "", out PRINT_HIEN_THI_TIEN_KHACH_DUA);


                Boolean PRINT_PRICE = false;
                Boolean PRINT_RECEIPT_TITLE = false;
                Boolean PRINT_PHONE_NUMBER = false;
                Boolean PRINT_STORE_NAME = false;
                Boolean PRINT_STORE_ADDRESS = false;
                Boolean PRINT_ORDER_NO = false;

                DataTable tbDM_MAYIN_BIENNHAN_CAUHINH = new thuvien().SP_SelectDataTable("SP_EXECUTESQL",
                                                                                    new string[] { "@sSQL" },
                                                                                    new object[] { "SELECT * FROM DM_MAYIN_BIENNHAN_CAUHINH" });

                if (tbDM_MAYIN_BIENNHAN_CAUHINH.Rows.Count > 0)
                {
                    LOGO_PATH = tbDM_MAYIN_BIENNHAN_CAUHINH.Rows[0]["LOGO_PATH"] + "";//??????????????????????????????????????????????????????????

                    HEADER = tbDM_MAYIN_BIENNHAN_CAUHINH.Rows[0]["HEADER"] + "";//
                    FOOTER = tbDM_MAYIN_BIENNHAN_CAUHINH.Rows[0]["FOOTER"] + "";//
                    int.TryParse(tbDM_MAYIN_BIENNHAN_CAUHINH.Rows[0]["MARIN_TOP"] + "", out MARIN_TOP);//
                    int.TryParse(tbDM_MAYIN_BIENNHAN_CAUHINH.Rows[0]["MARIN_BOTTOM"] + "", out MARIN_BOTTOM);//

                    Boolean.TryParse(tbDM_MAYIN_BIENNHAN_CAUHINH.Rows[0]["PRINT_PRICE"] + "", out PRINT_PRICE);//
                    Boolean.TryParse(tbDM_MAYIN_BIENNHAN_CAUHINH.Rows[0]["PRINT_RECEIPT_TITLE"] + "", out PRINT_RECEIPT_TITLE);//?????????????????
                    Boolean.TryParse(tbDM_MAYIN_BIENNHAN_CAUHINH.Rows[0]["PRINT_PHONE_NUMBER"] + "", out PRINT_PHONE_NUMBER);//
                    Boolean.TryParse(tbDM_MAYIN_BIENNHAN_CAUHINH.Rows[0]["PRINT_STORE_NAME"] + "", out PRINT_STORE_NAME);//
                    Boolean.TryParse(tbDM_MAYIN_BIENNHAN_CAUHINH.Rows[0]["PRINT_STORE_ADDRESS"] + "", out PRINT_STORE_ADDRESS);//
                    Boolean.TryParse(tbDM_MAYIN_BIENNHAN_CAUHINH.Rows[0]["PRINT_ORDER_NO"] + "", out PRINT_ORDER_NO);//
                    TITLE = tbDM_MAYIN_BIENNHAN_CAUHINH.Rows[0]["TITLE"] + "";
                    ADDRESS = tbDM_MAYIN_BIENNHAN_CAUHINH.Rows[0]["ADDRESS"] + "";
                    PHONE = tbDM_MAYIN_BIENNHAN_CAUHINH.Rows[0]["PHONE"] + "";
                }
                #endregion

                #region "   MARIN_TOP"
                for (int iMarin = 0; iMarin < MARIN_TOP; iMarin++)
                    psrlCOMport.WriteLine("");
                #endregion

                #region "   in LOGO"
                psrlCOMport.Write(((Char)(28)).ToString() + ((Char)(112)).ToString() + ((Char)(1)).ToString() + ((Char)(0)).ToString());
                #endregion

                #region "   HEADER"
                //HEADER
                psrlCOMport.Write(Print_Align_Left);
                psrlCOMport.Write(Print_Font_Normal_39CharPerRow); //in chữ thường 40chữ/dòng]

                if (HEADER != "")
                    psrlCOMport.WriteLine(HEADER);
                #endregion

                #region "   1.TEN QUAN - TITLE"
                //////////////////////////////TEN QUAN
                psrlCOMport.Write(Print_Align_Center);
                psrlCOMport.Write(Print_Double_Height);
                if (TITLE != "" && PRINT_RECEIPT_TITLE)//PRINT_STORE_NAME
                {
                    psrlCOMport.WriteLine("");
                    psrlCOMport.WriteLine(ocpublic.RemoveSign4VietnameseString(TITLE.Trim(' ')));
                    psrlCOMport.WriteLine("");
                }
                // -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# 
                // -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# 
                #endregion

                #region "   2.DIA CHI QUÁN - ADDRESS"
                psrlCOMport.Write(Print_Align_Left);
                psrlCOMport.Write(Print_Font_Normal_39CharPerRow); //in chữ thường 40chữ/dòng]

                ///////////////////////////////DIA CHI QUÁN
                ADDRESS = ADDRESS.Trim();
                if (ADDRESS != "" && PRINT_STORE_ADDRESS)
                    psrlCOMport.WriteLine(ocpublic.RemoveSign4VietnameseString(ADDRESS.Trim(' ')));
                #endregion

                #region "   3.SO DIEN THOAI - PHONE"
                ///////////////////////////////SO DIEN THOAI
                PHONE = PHONE.Trim();
                if (PHONE != "" && PRINT_PHONE_NUMBER)
                    psrlCOMport.WriteLine(ocpublic.RemoveSign4VietnameseString(PHONE.Trim(' ')));
                #endregion

                #region "   4.PHIEU"
                ///////////////////////////////================================================
                psrlCOMport.Write(Print_Align_Center);
                psrlCOMport.Write(Print_Font_Normal_39CharPerRow);

                //psrlCOMport.Write(((char)(29)).ToString() + ((char)(92)).ToString() + '\n' + ((char)(1)).ToString());
                psrlCOMport.Write(Print_Align_Center);
                psrlCOMport.Write(Print_Font_Normal_39CharPerRow);
                if ((ADDRESS != "" && PRINT_STORE_ADDRESS) || (PHONE != "" && PRINT_PHONE_NUMBER))
                    psrlCOMport.WriteLine(new String('=', 42));

                // -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# 
                // -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# 

                ///////////////////////////////PHIEU
                string sHD = _drPhieu["OBJID"] + "";
                if (!PRINT_ORDER_NO)
                    sHD = "";
                else
                    sHD = sLableHoaDon + ":" + sHD;

                psrlCOMport.WriteLine(this.f_splitString2Column(sLableBan_Phong + ": " + ocpublic.RemoveSign4VietnameseString(_drPhieu["OBJNAME"] + ""), sHD, 42));

                //sHD = int.Parse(sHD.Substring(4)) + "";
                psrlCOMport.WriteLine(this.f_splitString2Column(sLableNgayGio_Vao + ":", _drPhieu["NGAYHVao"] + "", 42));
                if (ocpublic.Registry_Get("rbtHienThiGioRa") == "True")
                    psrlCOMport.WriteLine(this.f_splitString2Column(sLableNgayGio_Ra + ":", _drPhieu["NGAYHRa"] + "", 42));

                // -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# 
                // -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# 
                #endregion

                #region "   4A.KHÁCH HÀNG"
                Boolean bChon_KH_Khi_Vao_Ban = false;
                
                Boolean.TryParse(ocpublic.Registry_Get(omh_dmkhachhang.Name + omh_dmkhachhang.chkChon_KH_Khi_Vao_Ban.Name), out bChon_KH_Khi_Vao_Ban);
                if (bChon_KH_Khi_Vao_Ban)
                {
                    string sKH_Ten = _drPhieu["DM_KHACHHANG_TEN"] + "";
                    if (sKH_Ten == "")
                        sKH_Ten = _drPhieu["NGUOILIENHE"] + "";
                    if (sKH_Ten != "")
                    {
                        string sKH_Sdt = _drPhieu["DM_KHACHHANG_DTDD"] + "";
                        if (sKH_Sdt == "")
                            sKH_Sdt = _drPhieu["DM_KHACHHANG_DTB"] + "";

                        if (sKH_Sdt != "")
                            sKH_Sdt = sLableDienThoai + ": " + sKH_Sdt;

                        psrlCOMport.WriteLine(this.f_splitString2Column(sLableKhachHang + ": " + ocpublic.RemoveSign4VietnameseString(sKH_Ten), sKH_Sdt, 42));
                    }
                }
                #endregion

                psrlCOMport.WriteLine(new String('=', 42));

                #region "   5.CHI TIET PHIEU"
                ///////////////////////////////CHI TIET PHIEU
                psrlCOMport.Write(Print_Align_Left);
                //int i1 = 32, i2 = 7, i3 = 17;   // = 56
                //int i1 = 23, i2 = 5, i3 = 11;   // = 42
                int i1 = 26, i2 = 5, i3 = 11;   // = 42
                string sGia_Ten = sLableGia;
                if (!PRINT_PRICE)
                    sGia_Ten = "";
                psrlCOMport.WriteLine(this.f_splitString4Column(sLableHangHoa, sGia_Ten, sLableSoLuong, sLableThanhTien_CT, i1, i2, i3));
                psrlCOMport.WriteLine(new String('-', 42));

                string sTenHang = "";
                string sDonGia = "";
                string sSoLuong = "";
                string sThanhTien = "";
                string sCK_Giam = "";
                //string sVAT_Tang = "";

                int iVAT_TMP = 0;
                Boolean iPrintVAT = false;
                if (_tbPhieuCT.Rows.Count > 0)//neu % VAT tat ca deu = nhau thi ko in
                {
                    int.TryParse(_tbPhieuCT.Rows[0]["VAT_PHANTRAM"] + "", out iVAT_TMP);
                    iPrintVAT = !(_tbPhieuCT.Select("VAT_PHANTRAM=" + iVAT_TMP).Length == _tbPhieuCT.Rows.Count);
                }
                int iCHIETKHAU_TMP = 0;
                Boolean iPrintCHIETKHAU = false;
                if (_tbPhieuCT.Rows.Count > 0)//neu % CHIET KHAU tat ca deu = nhau thi ko in
                {
                    int.TryParse(_tbPhieuCT.Rows[0]["CHIETKHAU_PHANTRAM"] + "", out iCHIETKHAU_TMP);
                    iPrintCHIETKHAU = !(_tbPhieuCT.Select("CHIETKHAU_PHANTRAM=" + iCHIETKHAU_TMP).Length == _tbPhieuCT.Rows.Count);
                }
                // chuongtohoai
                for (int _ir = 0; _ir < _tbPhieuCT.Rows.Count; _ir++)
                {
                    sTenHang = "";
                    sDonGia = "";
                    sSoLuong = "";
                    sThanhTien = "";
                    /////////////////
                    sCK_Giam = float.Parse(_tbPhieuCT.Rows[_ir]["CHIETKHAU_PHANTRAM"] + "").ToString("N0");

                    string[] gio = new string[0];
                    #region "   Tien_Gio_Them"
                    if (_drPhieu["DM_LOAIDICHVU_MA"] + "" == "1")//Karaoke
                    {
                        if (_tbPhieuCT.Rows[_ir]["GHI_CHU"] + "" != "" && _tbPhieuCT.Rows[_ir]["MAHH"] + "" == "Tien_Gio_Them")
                            sLableTienGio_Them = _tbPhieuCT.Rows[_ir]["GHI_CHU"] + "";
                    }
                    else if (_drPhieu["DM_LOAIDICHVU_MA"] + "" == "2")//Hotel
                    {
                        if (_tbPhieuCT.Rows[_ir]["GHI_CHU"] + "" != "" && _tbPhieuCT.Rows[_ir]["MAHH"] + "" == "Tien_Gio_Them")
                        {
                            sLableTienGio_Them = _tbPhieuCT.Rows[_ir]["GHI_CHU"] + "";
                            gio = sLableTienGio_Them.Split('h');
                        }
                    }
                    else if (_drPhieu["DM_LOAIDICHVU_MA"] + "" == "5")//Biza
                    {
                        if (_tbPhieuCT.Rows[_ir]["GHI_CHU"] + "" != "" && _tbPhieuCT.Rows[_ir]["MAHH"] + "" == "Tien_Gio_Them")
                            sLableTienGio_Them = _tbPhieuCT.Rows[_ir]["GHI_CHU"] + "";
                    }
                    #endregion

                    #region "   sTenHang"
                    if (sCK_Giam != "" && sCK_Giam != "0" && iPrintCHIETKHAU && sCK_Giam.IndexOf("100") == 0)
                    {//tamg
                        if (sLableTienGio_Them != "" && string.Concat(_tbPhieuCT.Rows[_ir]["MAHH"], "").IndexOf("Tien_Gio_Them") == 0)
                        {
                            if (gio.Length == 2)
                                if (string.Concat(gio[1], "").Trim() != "")
                                    //sTenHang = string.Concat(gio[1], "").Trim().Replace(" - ", "-") + " (" + string.Concat(gio[0], "h").Trim() + ")" + " --Mien Phi";
                                    sTenHang = sLableTienPhong_Gio + " (" + string.Concat(gio[0], "h").Trim() + ")" + " --Mien Phi";

                            if (sTenHang == "")
                                sTenHang = sLableTienGio_Them;
                        }
                        else if (sLableTienPhong_Gio != "" && string.Concat(_tbPhieuCT.Rows[_ir]["MAHH"], "").IndexOf("Tien_Gio") == 0)
                            sTenHang = sLableTienPhong_Gio + " --Mien Phi";
                        else
                            sTenHang = ocpublic.RemoveSign4VietnameseString(_tbPhieuCT.Rows[_ir]["OBJNAME"] + "").Trim() + " --Mien Phi";
                    }
                    else
                    {//ko tamg
                        if (sLableTienGio_Them != "" && string.Concat(_tbPhieuCT.Rows[_ir]["MAHH"], "").IndexOf("Tien_Gio_Them") == 0)
                        {
                            if (gio.Length == 2)
                                if (string.Concat(gio[1], "").Trim() != "")
                                    //sTenHang = string.Concat(gio[1], "").Trim().Replace(" - ", "-") + " (" + string.Concat(gio[0], "h").Trim() + ")";
                                    sTenHang = sLableTienPhong_Gio + " (" + string.Concat(gio[0], "h").Trim() + ")";

                            if (sTenHang == "")
                                sTenHang = sLableTienGio_Them;
                        }
                        else if (sLableTienPhong_Gio != "" && string.Concat(_tbPhieuCT.Rows[_ir]["MAHH"], "").IndexOf("Tien_Gio") == 0)
                            sTenHang = sLableTienPhong_Gio;
                        else
                            sTenHang = ocpublic.RemoveSign4VietnameseString(_tbPhieuCT.Rows[_ir]["OBJNAME"] + "").Trim();
                    }
                    #endregion

                    if (_tbPhieuCT.Rows[_ir]["DON_GIA"] + "" != "")
                        sDonGia = int.Parse(_tbPhieuCT.Rows[_ir]["DON_GIA"] + "").ToString("N0");

                    if (_tbPhieuCT.Rows[_ir]["SO_LUONG"] + "" != "")
                    {
                        if (_tbPhieuCT.Rows[_ir]["SO_LUONG"].ToString().IndexOf(".") > 0)
                            sSoLuong = float.Parse(_tbPhieuCT.Rows[_ir]["SO_LUONG"] + "").ToString("N2");
                        else
                            sSoLuong = float.Parse(_tbPhieuCT.Rows[_ir]["SO_LUONG"] + "").ToString("N0");
                        if (gio.Length == 2)
                            if (string.Concat(gio[0], "").Trim() != "")
                                sSoLuong = "";// string.Concat(gio[0], "h").Trim();
                    }

                    if (_tbPhieuCT.Rows[_ir]["THANH_TIEN"] + "" != "")
                        sThanhTien = float.Parse(_tbPhieuCT.Rows[_ir]["THANH_TIEN"] + "").ToString("N0");


                    string sTenHang2 = "";
                    #region "   sTenHang2"
                    if (sTenHang.Length + sDonGia.Length >= i1)
                    {
                        int iSpace = 0;
                        int icount = 0;
                        while (icount + sDonGia.Length < i1)
                        {
                            if (sTenHang.IndexOf(' ', icount) == icount)
                                iSpace = icount;
                            icount++;
                        }
                        string sTmp = "";

                        sTmp = sTenHang.Substring(0, iSpace).Trim();
                        sTenHang2 = sTenHang.Substring(iSpace).Trim();
                        sTenHang = sTmp;

                    }
                    #endregion


                    if (!PRINT_PRICE)
                        sDonGia = "";

                    psrlCOMport.WriteLine(this.f_splitString4Column(sTenHang, sDonGia, sSoLuong, sThanhTien, i1, i2, i3));

                    if (sTenHang2 != "")
                        psrlCOMport.WriteLine(sTenHang2);

                    #region "   sCK_Giam - sVAT_Tang"
                    if (sCK_Giam != "" && sCK_Giam != "0" && iPrintCHIETKHAU && sCK_Giam.IndexOf("100") < 0)
                    {
                        psrlCOMport.WriteLine(this.f_splitString2Column("--" + sLableGiamGia + "(" + sCK_Giam + "%)", "-" + float.Parse(_tbPhieuCT.Rows[_ir]["CHIETKHAU_TIEN"] + "").ToString("N0"), 42));
                    }
                    //sVAT_Tang = float.Parse(_tbPhieuCT.Rows[_ir]["VAT_TIEN"] + "").ToString("N0");
                    //if (sVAT_Tang != "" && sVAT_Tang != "0" && iPrintVAT)
                    //    psrlCOMport.WriteLine(this.f_splitString2Column("--" + sLableVAT, sVAT_Tang, 42));
                    #endregion

                }


                // -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# 
                // -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# 
                #endregion

                #region "   6.PHIEU.TINH TIEN"
                /////////////////////////////// FOOT PAGE
                psrlCOMport.WriteLine(new String('-', 42));
                string sChietKhau_PhanTram = "";
                //string sVAT_PhanTram = "";
                string sTienTip_PhanTram = "";
                string sPhiDichVu_PhanTram = "";
                string sVAT_phantram = "";
                string sChietKhau = "";
                string sVAT = "";
                string sTienTip = "";
                string sPhiDichVu = "";
                string sPhuThuKhac = "0";
                int iDAT_COC = 0;
                int iKhuyenMai = 0;

                // -==# -==# -==# -==# -==# -==# -==#CK -==# -==# -==# -==# -==# -==# -==# -==# -==# -==#
                //if (iPrintCHIETKHAU == false)
                //{//
                //    _drPhieu["CK_PHANTRAM"] = iCHIETKHAU_TMP;
                //    if (_drPhieu["CK_PHANTRAM"] + "" != "0" && _drPhieu["CK_PHANTRAM"] + "" != "")
                //        sChietKhau_PhanTram = " (" + _drPhieu["CK_PHANTRAM"] + "%)";
                //}
                
                sChietKhau = (int.Parse(_drPhieu["CK_TIEN"] + "") + int.Parse(_drPhieu["CK_THUCAN_TIEN"] + "") + int.Parse(_drPhieu["CK_THUCUONG_TIEN"] + "")).ToString("N0");
                if (sChietKhau == "0")
                    sChietKhau = "0";
                // -==# -==# -==# -==# -==# -==# -==#VAT -==# -==# -==# -==# -==# -==# -==# -==# -==# -==#

                if (iPrintVAT == false)
                {
                    _drPhieu["VAT_PHANTRAM"] = iVAT_TMP;
                    if (_drPhieu["VAT_PHANTRAM"] + "" != "0" && _drPhieu["VAT_PHANTRAM"] + "" != "")
                        sVAT_phantram = " (" + _drPhieu["VAT_PHANTRAM"] + "%)";
                }
                if (_drPhieu["VAT_TIEN"] + "" != "0" && _drPhieu["VAT_TIEN"] + "" != "")
                    sVAT = int.Parse(_drPhieu["VAT_TIEN"] + "").ToString("N0");

                // -==# -==# -==# -==# -==# -==# -==#TTIEP -==# -==# -==# -==# -==# -==# -==# -==# -==# -==#

                if (_drPhieu["TTIEP_PHANTRAM"] + "" != "0" && _drPhieu["TTIEP_PHANTRAM"] + "" != "")
                    sTienTip_PhanTram = " (" + _drPhieu["TTIEP_PHANTRAM"] + "%)";

                if (_drPhieu["TTIEP_TIEN"] + "" != "0" && _drPhieu["TTIEP_TIEN"] + "" != "")
                    sTienTip = int.Parse(_drPhieu["TTIEP_TIEN"] + "").ToString("N0");

                // -==# -==# -==# -==# -==# -==# -==#PHU_THU -==# -==# -==# -==# -==# -==# -==# -==# -==# -==#

                if (_drPhieu["PHU_THU_PHANTRAM"] + "" != "0" && _drPhieu["PHU_THU_PHANTRAM"] + "" != "")
                    sPhiDichVu_PhanTram = " (" + _drPhieu["PHU_THU_PHANTRAM"] + "%)";

                // tohoaichuong

                if (_drPhieu["VAT_PHANTRAM"] + "" != "0" && _drPhieu["VAT_PHANTRAM"] + "" != "")
                    sVAT_phantram = " (" + _drPhieu["VAT_PHANTRAM"] + "%)";



                if (_drPhieu["PHU_THU"] + "" != "0" && _drPhieu["PHU_THU"] + "" != "")
                    sPhiDichVu = int.Parse(_drPhieu["PHU_THU"] + "").ToString("N0");

                if (_drPhieu["PHU_THU_KHAC"] + "" != "0" && _drPhieu["PHU_THU_KHAC"] + "" != "")
                    sPhuThuKhac = int.Parse(_drPhieu["PHU_THU_KHAC"] + "").ToString("N0");

                //if (_drPhieu["DAT_COC"] + "" != "0" && _drPhieu["DAT_COC"] + "" != "")
                int.TryParse(_drPhieu["DAT_COC"] + "", out iDAT_COC);

                int.TryParse(_drPhieu["KM_TIEN"] + "", out iKhuyenMai);

                // -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==#

                if (sChietKhau != "" || sVAT != "" || sTienTip != "" || sPhiDichVu != "" || iDAT_COC > 0 || sPhuThuKhac != "0" || iKhuyenMai > 0)
                {
                    //1.THANH TIEN
                    // chuongtohoai 123
                    //psrlCOMport.WriteLine(this.f_splitString2Column("" + sLableTongTien1, float.Parse(_drPhieu["THANH_TIEN"] + ""), 42));//
                    psrlCOMport.WriteLine(this.f_splitString2Column("" + sLableTongTien1, float.Parse(_drPhieu["THANH_TIEN"] + "").ToString("N0"), 42));//

                    //2.CHIET KHAU
                    if (sChietKhau != "" && sChietKhau != "0")
                        psrlCOMport.WriteLine(this.f_splitString2Column(sLableGiamGia + sChietKhau_PhanTram, "-" + sChietKhau + "", 42));

                    //3.PHI DICH VU
                    if (sPhiDichVu != "" && sPhiDichVu != "0")
                        psrlCOMport.WriteLine(this.f_splitString2Column(sLablePhiDichVu + sPhiDichVu_PhanTram, sPhiDichVu + "", 42));

                    //4.KHUYEN MAI
                    if (iKhuyenMai > 0)
                    {
                        string sKM_PHANTRAM = "";
                        if (_drPhieu["KM_PHANTRAM"] + "" != "" && _drPhieu["KM_PHANTRAM"] + "" != "0")
                            sKM_PHANTRAM = " (" + _drPhieu["KM_PHANTRAM"] + "%)";
                        psrlCOMport.WriteLine(this.f_splitString2Column(sLableKhuyenMai + sKM_PHANTRAM, "-"+iKhuyenMai.ToString("N0") + "", 42));//20110212
                    }

                    //5.PHU THU KHAC
                    if (sPhuThuKhac != "0")
                    {
                        if (sPhuThuKhac.IndexOf("-") >= 0)
                            sLablePhuThuKhac = sLableGiamGia;
                        psrlCOMport.WriteLine(this.f_splitString2Column(sLablePhuThuKhac, sPhuThuKhac + "", 42));//Phu thu khac
                    }

                    //6.VAT
                    if (sVAT != "" && sVAT != "0")
                    {
                        #region "   sLableTongTien1"
                        if (sChietKhau == "")
                            sChietKhau = "0";

                        if (sPhiDichVu == "")
                            sPhiDichVu = "0";

                        if (sPhuThuKhac == "")
                            sPhuThuKhac = "0";

                        if ((sChietKhau != "0")
                            ||
                            (sPhiDichVu != "0")
                            ||
                            sPhuThuKhac != "0"
                            ||
                            iKhuyenMai > 0
                            )
                        {
                            int sp = int.Parse(_drPhieu["THANH_TIEN"] + "")
                                     - int.Parse(sChietKhau.Replace(",", ""))
                                     + int.Parse(sPhiDichVu.Replace(",", ""))
                                     + int.Parse(sPhuThuKhac.Replace(",", ""))
                                     - iKhuyenMai
                                     ;

                            if (sp > 0)
                            {
                                psrlCOMport.WriteLine(new String('-', 42));
                                psrlCOMport.WriteLine(this.f_splitString2Column("" + sLableTongTien1, sp.ToString("N0"), 42));
                            }
                        }
                        #endregion
                        //
                        psrlCOMport.WriteLine(this.f_splitString2Column(sLableVAT + sVAT_phantram, sVAT + "", 42));
                    }
                    ////7.DAT COC
                    //if (iDAT_COC > 0)
                    //    psrlCOMport.WriteLine(this.f_splitString2Column(sLableTienDatCoc, iDAT_COC.ToString("N0") + "", 42));

                    //8.TIP
                    if (sTienTip != "" && sTienTip != "0")
                        psrlCOMport.WriteLine(this.f_splitString2Column(sLableTienTIPs + sTienTip_PhanTram, sTienTip + "", 42));
                    ///
                    psrlCOMport.WriteLine(new String('-', 42));
                }
                int iTONG_TIEN = 0;
                int.TryParse(_drPhieu["TONG_TIEN"] + "", out iTONG_TIEN);
                psrlCOMport.Write(Print_Font_Double_Height);
                //psrlCOMport.WriteLine(this.f_splitString2Column("Final total", iTONG_TIEN.ToString("N0"), 42));                
                psrlCOMport.WriteLine(this.f_splitString2Column(sLableTongTien2, iTONG_TIEN.ToString("N0"), 42));                


                psrlCOMport.Write(Print_Font_Normal_39CharPerRow);

                int iTIEN_DA_NHAN = 0;
                int.TryParse(_drPhieu["TIEN_DA_NHAN"] + "", out iTIEN_DA_NHAN);
                if (iTIEN_DA_NHAN > 0)
                {
                    if (iDAT_COC > 0)
                        psrlCOMport.WriteLine(this.f_splitString2Column(sLableTienDatCoc, iDAT_COC.ToString("N0") + "", 42));

                    if (PRINT_HIEN_THI_TIEN_KHACH_DUA && iTIEN_DA_NHAN > 0)
                        psrlCOMport.WriteLine(this.f_splitString2Column(sLableDaNhan, iTIEN_DA_NHAN.ToString("N0"), 42));

                    if (PRINT_HIEN_THI_TIEN_THOI)
                    {
                        if ((iTIEN_DA_NHAN + iDAT_COC) > iTONG_TIEN)
                            psrlCOMport.WriteLine(this.f_splitString2Column(sLableConLai, (iDAT_COC + iTIEN_DA_NHAN - iTONG_TIEN).ToString("N0"), 42));
                        else if ((iTIEN_DA_NHAN + iDAT_COC) < iTONG_TIEN)
                                psrlCOMport.WriteLine(this.f_splitString2Column("Tien no", (iDAT_COC + iTIEN_DA_NHAN - iTONG_TIEN).ToString("N0"), 42));
                    }
                }
                else if (iDAT_COC > 0)
                {
                    psrlCOMport.WriteLine(this.f_splitString2Column(sLableTienDatCoc, iDAT_COC.ToString("N0") + "", 42));

                    if (PRINT_HIEN_THI_TIEN_KHACH_DUA && iTIEN_DA_NHAN>0)
                        psrlCOMport.WriteLine(this.f_splitString2Column(sLableDaNhan, iTIEN_DA_NHAN.ToString("N0"), 42));

                    if (PRINT_HIEN_THI_TIEN_THOI)
                    {
                        //if ((iDAT_COC) > iTONG_TIEN)
                        psrlCOMport.WriteLine(this.f_splitString2Column(sLableConLai, (iDAT_COC - iTONG_TIEN).ToString("N0"), 42));
                        //else
                        //    psrlCOMport.WriteLine(this.f_splitString2Column("CON THIEU", (iDAT_COC - iTONG_TIEN).ToString("N0"), 42));
                    }
                }

                #region "   GHI_CHU"
                if (_drPhieu["GHI_CHU"] + "" != "")
                {//42
                    string sGhiChu = ocpublic.RemoveSign4VietnameseString(_drPhieu["GHI_CHU"] + "").Trim();
                    string sGhiChu2 = "";
                    #region "   sGhiChu2"
                    if (sGhiChu.Length>= 42)
                    {
                        int iSpace = 0;
                        int icount = 0;
                        while (icount< 42)
                        {
                            if (sGhiChu.IndexOf(' ', icount) == icount)
                                iSpace = icount;
                            icount++;
                        }
                        string sTmp = "";

                        sTmp = sGhiChu.Substring(0, iSpace).Trim();
                        sGhiChu2 = sGhiChu.Substring(iSpace).Trim();
                        sGhiChu = sTmp;

                    }
                    #endregion
                    if (sGhiChu != "")
                        psrlCOMport.WriteLine(sGhiChu);
                    if (sGhiChu2 != "")
                        psrlCOMport.WriteLine(sGhiChu2);

                }
                #endregion

                #endregion

                #region "   7.CHAN TRANG.2"
                // -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# 
                // -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# -==# 
                /////////////////////////////// ========================================
                psrlCOMport.WriteLine(new String('=', 42));
                psrlCOMport.WriteLine(sLableNhanVien + ": " + ocpublic.RemoveSign4VietnameseString(_drPhieu["NGUOI_LAP"] + ""));
                #endregion

                #region "   FOOTER"
                //HEADER
                psrlCOMport.Write(Print_Align_Center);
                psrlCOMport.Write(Print_Font_Normal_39CharPerRow); //in chữ thường 40chữ/dòng]
                if (FOOTER != "")
                    psrlCOMport.WriteLine(FOOTER);
                #endregion

                #region "   MARIN_BOTTOM"
                for (int iMarin = 0; iMarin < MARIN_BOTTOM; iMarin++)
                    psrlCOMport.WriteLine("");
                #endregion

                psrlCOMport.WriteLine(((char)(29)).ToString() + ((char)(86)).ToString() + ((char)(66)).ToString() + ((char)(1)).ToString()); //Ngắt trang
            }
            catch (Exception ex)
            {
                LPV_POS.Module.MessageBox_LPV.Show("In BILL không thành công.\n" + ex.Message, string.Empty, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            #endregion
        }

        #region "   f_splitString4Column"
        string f_splitString4Column(string a, string b, string c, string d, int i1, int i2, int i3)
        {//i1: 32; i2:7; i3: 17
            string str = "";
            str = a;
            str += new cpublic().Spacestring(i1 - a.Length - b.Length);
            str += b;
            str += new cpublic().Spacestring(i2 - c.Length);
            str += c;
            str += new cpublic().Spacestring(i3 - d.Length);
            str += d;
            return str;
        }
        #endregion

        #region "   f_splitString2Column"
        string f_splitString2Column(string a, string b, int i)
        {//i: 56
            string str = "";
            str = a;
            str += new cpublic().Spacestring(i - a.Length - b.Length);
            str += b;
            return str;
        }
        #endregion

        #endregion---------------end IN BILL TINH DIEM KHACH HANG ----------------

        #region "   getTable_Phieu"
        private DataTable getTable_Phieu(string maBan, string maPhieu)
        {
            string sSQL = "";
            if (maBan != "")
                maBan = " AND P.DM_BAN_MA LIKE '" + maBan + "' ";
            if (maPhieu != "")
                maPhieu = " AND p.OBJID LIKE '" + maPhieu + "'";

            sSQL = "SELECT DISTINCT b.OBJNAME, p.OBJID, p.DM_BAN_MA, NGUOI_LAP = ISNULL(n.OBJNAME, p.NGUOI_LAP), p.NGAY_GIO_VAO, p.NGAY_GIO_RA, " +
                   "        p.TINH_TIEN_THEO_NGAY, p.TIEN_GIO, p.THANH_TIEN, p.CK_PHANTRAM, p.CK_TIEN, " +
                   "        CK_THUCAN_PHANTRAM = ISNULL(p.CK_THUCAN_PHANTRAM, 0), CK_THUCAN_TIEN = ISNULL(p.CK_THUCAN_TIEN, 0), " +//20111110
                   "        CK_THUCUONG_PHANTRAM = ISNULL(p.CK_THUCUONG_PHANTRAM, 0), CK_THUCUONG_TIEN = ISNULL(p.CK_THUCUONG_TIEN, 0), " +//20111110

                   "        p.VAT_PHANTRAM, p.VAT_TIEN, p.TTIEP_PHANTRAM, p.TTIEP_TIEN, " +
                   "        p.PHU_THU_PHANTRAM, p.PHU_THU, p.PHU_THU_KHAC, p.TONG_TIEN, p.GHI_CHU, " +
                   "        NGAYHVao = CONVERT(VARCHAR(30), p.NGAY_GIO_VAO, 103) +' '+ CONVERT(VARCHAR(5), p.NGAY_GIO_VAO, 108), " +
                   "        NGAYHRa = CONVERT(VARCHAR(30), p.NGAY_GIO_RA, 103) +' '+ CONVERT(VARCHAR(5), p.NGAY_GIO_RA, 108), " +
                   "        p.TIEN_DA_NHAN, " +
                   "        DAT_COC = (SELECT DAT_COC FROM DM_BAN_DAT_BAN WHERE BH_PHIEU_ID = p.OBJID ), " +
                   "        KM_TIEN, KM_PHANTRAM, " +
                   "        DM_KHACHHANG_TEN = dk.OBJNAME, " +
                   "        dk.NGUOILIENHE, " +
                   "        DM_KHACHHANG_DTB = dk.DIENTHOAIBAN, " +
                   "        DM_KHACHHANG_DTDD = dk.DIENTHOAIDD, " +
                   "        b.DM_LOAIDICHVU_MA " +
                   "FROM BH_PHIEU p " +
                   "        LEFT JOIN DM_BAN b ON p.DM_BAN_MA = b.OBJID " +
                   "        LEFT JOIN DM_DANGNHAP d ON p.NGUOI_LAP = d.USERNAME " +
                   "        LEFT JOIN DM_NHANVIEN n ON d.MA_NHANVIEN = n.OBJID " +
                   "        LEFT JOIN DM_KHACHHANG dk ON p.CUSTOMER_ID = dk.THEKHACHHANG " +
                   "WHERE 1 = 1 " + maBan + maPhieu
                ;
            DataTable _tbPhieu = new thuvien().SP_SelectDataTable("SP_EXECUTESQL", new string[] { "@sSQL" }, new object[] { sSQL });
            return _tbPhieu;
        }
        #endregion
    }
}